import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeIndex from '@/views/HomeIndex.vue'
import aaa from '@/views/home/aaa.vue'
import Login from '@/views/Login.vue'
import UserList from '@/views/User/List.vue'
import UserAdd from '@/views/User/Add.vue'
import AuthAdd from '@/views/Auth/Authadd.vue'
import AuthList from '@/views/Auth/Authlist.vue'
import RoleList from '@/views/Role/RoleList.vue'
import RoleAdd from '@/views/Role/RoleAdd.vue'
import RoleEdit from '@/views/Role/RoleEdit.vue'
import DishList from '@/views/Dish/DishList.vue'
import DishAdd from '@/views/Dish/DishAdd.vue'
import CatAdd from '@/views/Cat/CatAdd.vue'
import CatList from '@/views/Cat/CatList.vue'
import ImportExcel from '@/views/Excel/ImportExcel.vue'
import ExportExcel from '@/views/Excel/ExportExcel.vue'
import Map1 from '@/views/map/map.vue'
import Not from '@/views/Not.vue'
import { getCookie } from '@/utils/cookie'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
    {
      path: '/',
      redirect: '/homeindex/aaa'
    },
    {
      path:'/homeindex',
      component:HomeIndex,
      children:[
        {
          path: 'aaa',
          component: aaa
        },
        {
          path: '/user/list',
          component: UserList
        },
        {
          path: '/user/add',
          component: UserAdd
        },
        {
          path: '/auth/add',
          component: AuthAdd
        },
        {
          path: '/auth/list',
          component: AuthList
        },
        {
          path: '/role/list',
          component: RoleList
        },
        {
          path: '/role/add',
          component: RoleAdd
        },
        {
          path: '/role/edit/:id',
          component: RoleEdit
        },
        {
          path: '/dish/list',
          component: DishList
        },
        {
          path: '/dish/add',
          component: DishAdd
        },
        {
          path: '/cat/list',
          component: CatList
        },
        {
          path: '/cat/add',
          component: CatAdd
        },
        {
          path: '/excel/exportExcel',
          component: ExportExcel
        },
        {
          path: '/excel/importExcel',
          component: ImportExcel
        },
        {
          path: '/map/map1',
          component: Map1
        },
      ]
    },
    {
      path:'/login',
      component: Login
    },
    {
      path:'/404',
      component: Not
    },
    {
      path:'*',
      redirect:'/404'
    }
]

const router = new VueRouter({
  routes
})

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
}

// 路由守卫 - 判断用户是否登录
router.beforeEach((to,from,next)=>{
    // to  即将要跳转的路径
    if(to.path === '/login' || to.path === '/404'){
      // 放行
      next()
      return
    }
    // 获取cookie
    let adminname = getCookie('adminname')
    // 验证是否有cookie
    if(adminname){
      // console.log(1);
          // 有cookie  设置一下登陆状态
        store.commit('changeLoginStatus',true)
        // 判断一下 vuex登陆状态
        if(store.state.isLogin){
          // console.log(2);
          // 登陆了
          // 如果去首页 放行
          if(to.path === '/homeindex'){
            next()
            return
          }
          // 获取到当前用户拥有的权限
          // console.log(1);
          let auths = sessionStorage.getItem('auths')
          // 将所有的子权限的路径 - 组成一个数组
          auths = JSON.parse(auths)
          // 遍历
          // console.log(2);
          let arr = []
          auths.forEach(item => {
            item.children && item.children.length && arr.push(...(item.children.map(v => v.path)))
          })
          // console.log(3);
          // 判断to.path是否在数组中
          // 数to.path中有几个斜杠
            let count = 0
            let path = to.path
            for(let a = 0 ; a < to.path.length ; a++ ){
              // 如果这条路径有两个以上的/ 就把第三个/之前截取下来
              if(to.path[a] === '/'){
                  count++
                  if(count > 2){
                    path = to.path.slice(0,a)
                    break
                  }
              }
            }
            // console.log(4);
            // 判断path是否在数组中
            if(arr.includes(path)) {
            // console.log(5);
              next()
            } else {
              router.push('/404')
              return
            }
          next()
          return
        } else {
          router.push('/login')
          return
        }
    } else {
        router.push('/login')
        return
    }
    
})

export default router
